using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.SGI.ERP.Entidades;

namespace MakeIT.SGI.ERP.Negocios
{
	public partial class SgiDAOT : DAOT
	{

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte, decimal? valorDe, decimal? valorAte)
		{
			TitrecVO titrecVO = null;
			List<TitrecVO> listTitrecVOs = getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, valorDe, valorAte);
			if (listTitrecVOs != null && listTitrecVOs.Count > 0)
			{
				titrecVO = listTitrecVOs[0];
			}
			return titrecVO;
		}

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte, decimal? valorDe)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, valorDe, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, idCtaCor, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc, int? idFilial)
        {
            return getTitrec(idTitRec, nrDoc, idFilial, null);
        }

        public TitrecVO getTitrec(int? idTitRec, string nrDoc)
        {
            return getTitrec(idTitRec, nrDoc, null);
        }

        public TitrecVO getTitrec(int? idTitRec)
        {
            return getTitrec(idTitRec, null);
        }

        public List<TitrecVO> getTitrecs()
        {
            return getTitrecs(null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec)
        {
            return getTitrecs(idTitRec, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc)
        {
            return getTitrecs(idTitRec, nrDoc, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte, decimal? valorDe)
        {
            return getTitrecs(idTitRec, nrDoc, idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, valorDe, null);
        }

        public List<TitrecVO> getTitrecs(int? idTitRec, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idCliente, int? idFormaPgto, int? idPlnCta, int? idCLucro, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte, decimal? valorDe, decimal? valorAte)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT ttr.idTitRec as ttr_idTitRec, ttr.idFilial as ttr_idFilial, fil.filial as fil_filial, ttr.idCtaCor as ttr_idCtaCor, ctc.ctaCor as ctc_ctaCor, ttr.idCaixa as ttr_idCaixa, cxa.caixa as cxa_caixa, ttr.idCliente as ttr_idCliente, ttr.idFormaPgto as ttr_idFormaPgto, fpg.formaPgto as fpg_formaPgto, ttr.idPlnCta as ttr_idPlnCta, plc.plnCta as plc_plnCta, ttr.idCLucro as ttr_idCLucro, clc.cLucro as clc_cLucro, ttr.nrDoc as ttr_nrDoc, ttr.obs as ttr_obs, ttr.vcto as ttr_vcto, ttr.pgto as ttr_pgto, ttr.valor as ttr_valor, ttr.valorJuroC as ttr_valorJuroC, ttr.valorDescC as ttr_valorDescC, ttr.valorRec as ttr_valorRec, ttr.valorSaldo as ttr_valorSaldo, ttr.situacao as ttr_situacao, ttr.nrDocOri as ttr_nrDocOri, ttr.cadastro as ttr_cadastro, ttr.cadastradoPor as ttr_cadastradoPor, ttr.edicao as ttr_edicao, ttr.editadoPor as ttr_editadoPor  ");
            query.AppendLine("FROM titrec as ttr");
            query.AppendLine("  LEFT OUTER JOIN filial as fil on ttr.idFilial=fil.idFilial");
            query.AppendLine("      LEFT OUTER JOIN ctacor as ctc on ttr.idCtaCor=ctc.idCtaCor");
            query.AppendLine("          LEFT OUTER JOIN banco as ban on ctc.idBanco=ban.idBanco");
            query.AppendLine("          LEFT OUTER JOIN caixa as cxa on ttr.idCaixa=cxa.idCaixa");
            query.AppendLine("              LEFT OUTER JOIN cliente as cli on ttr.idCliente=cli.idCliente");
            query.AppendLine("                  LEFT OUTER JOIN formapgto as fpg on ttr.idFormaPgto=fpg.idFormaPgto");
            query.AppendLine("                      LEFT OUTER JOIN plncta as plc on ttr.idPlnCta=plc.idPlnCta");
            query.AppendLine("                          LEFT OUTER JOIN clucro as clc on ttr.idCLucro=clc.idCLucro  ");            
            
            if (idTitRec != null)
			{
				query.AppendLine(where + "ttr.idTitRec=@idTitRec ");
				where = "AND ";
			}
			if (idFilial != null)
			{
				query.AppendLine(where + "ttr.idFilial=@idFilial ");
				where = "AND ";
			}
			if (idCtaCor != null)
			{
				query.AppendLine(where + "ttr.idCtaCor=@idCtaCor ");
				where = "AND ";
			}
			if (idCaixa != null)
			{
				query.AppendLine(where + "ttr.idCaixa=@idCaixa ");
				where = "AND ";
			}
			if (idCliente != null)
			{
				query.AppendLine(where + "ttr.idCliente=@idCliente ");
				where = "AND ";
			}
			if (idFormaPgto != null)
			{
				query.AppendLine(where + "ttr.idFormaPgto=@idFormaPgto ");
				where = "AND ";
			}
			if (idPlnCta != null)
			{
				query.AppendLine(where + "ttr.idPlnCta=@idPlnCta ");
				where = "AND ";
			}
			if (idCLucro != null)
			{
				query.AppendLine(where + "ttr.idCLucro=@idCLucro ");
				where = "AND ";
			}
			if (nrDoc != null)
			{
				query.AppendLine(where + "ttr.nrDoc LIKE @nrDoc ");
				where = "AND ";
			}
			if (situacao != null)
			{
				query.AppendLine(where + "ttr.situacao=@situacao ");
				where = "AND ";
			}
			if (nrDocOri != null)
			{
				query.AppendLine(where + "ttr.nrDocOri=@nrDocOri ");
				where = "AND ";
			}

            if (idBanco != null)
            {
                query.AppendLine(where + "ctc.idBanco=@idBanco ");
                where = "AND ";
            }
            if (vctoDe != null)
            {
                query.AppendLine(where + "ttr.vcto>=@vctoDe ");
                where = "AND ";
            }
            if (vctoAte != null)
            {
                query.AppendLine(where + "ttr.vcto<=@vctoAte ");
                where = "AND ";
            }
            if (pgtoDe != null)
            {
                query.AppendLine(where + "ttr.pgto>=@pgtoDe ");
                where = "AND ";
            }
            if (pgtoAte != null)
            {
                query.AppendLine(where + "ttr.pgto<=@pgtoAte ");
                where = "AND ";
            }
            if (valorDe != null)
            {
                query.AppendLine(where + "ttr.valor>=@valorDe ");
                where = "AND ";
            }
            if (valorAte != null)
            {
                query.AppendLine(where + "ttr.valor<=@valorAte ");
                where = "AND ";
            }


			query.AppendLine("ORDER BY ttr.nrDoc ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			if (idTitRec!= null) cmd.Parameters.Add(getParameter(cmd, "@idTitRec", idTitRec));
			if (idFilial!= null) cmd.Parameters.Add(getParameter(cmd, "@idFilial", idFilial));
			if (idCtaCor!= null) cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", idCtaCor));
			if (idCaixa!= null) cmd.Parameters.Add(getParameter(cmd, "@idCaixa", idCaixa));
			if (idCliente!= null) cmd.Parameters.Add(getParameter(cmd, "@idCliente", idCliente));
			if (idFormaPgto!= null) cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", idFormaPgto));
			if (idPlnCta!= null) cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", idPlnCta));
			if (idCLucro!= null) cmd.Parameters.Add(getParameter(cmd, "@idCLucro", idCLucro));
			if (nrDoc!= null) cmd.Parameters.Add(getParameter(cmd, "@nrDoc", "%"+nrDoc+"%"));
			if (situacao!= null) cmd.Parameters.Add(getParameter(cmd, "@situacao", situacao));
			if (nrDocOri!= null) cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", nrDocOri));
            if (idBanco != null) cmd.Parameters.Add(getParameter(cmd, "@idBanco", idBanco));

            if (vctoDe != null)
            {
                DateTime data = new DateTime(vctoDe.Value.Year, vctoDe.Value.Month, vctoDe.Value.Day, 0, 0, 0);
                cmd.Parameters.Add(getParameter(cmd, "@vctoDe", data));
            }

            if (vctoAte != null)
            {
                DateTime data = new DateTime(vctoAte.Value.Year, vctoAte.Value.Month, vctoAte.Value.Day, 23, 59, 59);
                cmd.Parameters.Add(getParameter(cmd, "@vctoAte", data));
            }

            if (pgtoDe != null)
            {
                DateTime data = new DateTime(pgtoDe.Value.Year, pgtoDe.Value.Month, pgtoDe.Value.Day, 0, 0, 0);
                cmd.Parameters.Add(getParameter(cmd, "@pgtoDe", data));
            }

            if (pgtoAte != null)
            {
                DateTime data = new DateTime(pgtoAte.Value.Year, pgtoAte.Value.Month, pgtoAte.Value.Day, 23, 59, 59);
                cmd.Parameters.Add(getParameter(cmd, "@pgtoAte", data));
            }

            if (valorDe != null) cmd.Parameters.Add(getParameter(cmd, "@valorDe", valorDe));
            if (valorAte != null) cmd.Parameters.Add(getParameter(cmd, "@valorAte", valorAte));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<TitrecVO> listTitrec = new List<TitrecVO>();

				while (reader.Read())
				{
					TitrecVO titrecVO = TitrecVO.CreateVO();
					titrecVO.idTitRec = reader.GetInt32(reader.GetOrdinal("ttr_idTitRec"));
					titrecVO.idFilial = reader.GetInt32(reader.GetOrdinal("ttr_idFilial"));
					if (!reader.IsDBNull(reader.GetOrdinal("fil_filial")))
						titrecVO.idFilialVO.filial = reader.GetString(reader.GetOrdinal("fil_filial"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_idCtaCor")))
						titrecVO.idCtaCor = reader.GetInt32(reader.GetOrdinal("ttr_idCtaCor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ctc_ctaCor")))
						titrecVO.idCtaCorVO.ctaCor = reader.GetString(reader.GetOrdinal("ctc_ctaCor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_idCaixa")))
						titrecVO.idCaixa = reader.GetInt32(reader.GetOrdinal("ttr_idCaixa"));
					if (!reader.IsDBNull(reader.GetOrdinal("cxa_caixa")))
						titrecVO.idCaixaVO.caixa = reader.GetString(reader.GetOrdinal("cxa_caixa"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_idCliente")))
						titrecVO.idCliente = reader.GetInt32(reader.GetOrdinal("ttr_idCliente"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_idFormaPgto")))
						titrecVO.idFormaPgto = reader.GetInt32(reader.GetOrdinal("ttr_idFormaPgto"));
					if (!reader.IsDBNull(reader.GetOrdinal("fpg_formaPgto")))
						titrecVO.idFormaPgtoVO.formaPgto = reader.GetString(reader.GetOrdinal("fpg_formaPgto"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_idPlnCta")))
						titrecVO.idPlnCta = reader.GetInt32(reader.GetOrdinal("ttr_idPlnCta"));
					if (!reader.IsDBNull(reader.GetOrdinal("plc_plnCta")))
						titrecVO.idPlnCtaVO.plnCta = reader.GetString(reader.GetOrdinal("plc_plnCta"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_idCLucro")))
						titrecVO.idCLucro = reader.GetInt32(reader.GetOrdinal("ttr_idCLucro"));
					if (!reader.IsDBNull(reader.GetOrdinal("clc_cLucro")))
						titrecVO.idCLucroVO.cLucro = reader.GetString(reader.GetOrdinal("clc_cLucro"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_nrDoc")))
						titrecVO.nrDoc = reader.GetString(reader.GetOrdinal("ttr_nrDoc"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_obs")))
						titrecVO.obs = reader.GetString(reader.GetOrdinal("ttr_obs"));
					titrecVO.vcto = reader.GetDateTime(reader.GetOrdinal("ttr_vcto"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_pgto")))
						titrecVO.pgto = reader.GetDateTime(reader.GetOrdinal("ttr_pgto"));
					titrecVO.valor = reader.GetDecimal(reader.GetOrdinal("ttr_valor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_valorJuroC")))
						titrecVO.valorJuroC = reader.GetDecimal(reader.GetOrdinal("ttr_valorJuroC"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_valorDescC")))
						titrecVO.valorDescC = reader.GetDecimal(reader.GetOrdinal("ttr_valorDescC"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_valorRec")))
						titrecVO.valorRec = reader.GetDecimal(reader.GetOrdinal("ttr_valorRec"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_valorSaldo")))
						titrecVO.valorSaldo = reader.GetDecimal(reader.GetOrdinal("ttr_valorSaldo"));
					titrecVO.situacao = reader.GetChar(reader.GetOrdinal("ttr_situacao"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttr_nrDocOri")))
						titrecVO.nrDocOri = reader.GetString(reader.GetOrdinal("ttr_nrDocOri"));
					titrecVO.cadastro = reader.GetDateTime(reader.GetOrdinal("ttr_cadastro"));
					titrecVO.cadastradoPor = reader.GetString(reader.GetOrdinal("ttr_cadastradoPor"));
					titrecVO.edicao = reader.GetDateTime(reader.GetOrdinal("ttr_edicao"));
					titrecVO.editadoPor = reader.GetString(reader.GetOrdinal("ttr_editadoPor"));


					listTitrec.Add(titrecVO);
				}
				reader.Close();
				return listTitrec;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void setTitrec(TitrecVO titrecVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE titrec ");
			query.AppendLine("SET idFilial=@idFilial, idCtaCor=@idCtaCor, idCaixa=@idCaixa, idCliente=@idCliente, idFormaPgto=@idFormaPgto, idPlnCta=@idPlnCta, idCLucro=@idCLucro, nrDoc=@nrDoc, obs=@obs, vcto=@vcto, pgto=@pgto, valor=@valor, valorJuroC=@valorJuroC, valorDescC=@valorDescC, valorRec=@valorRec, valorSaldo=@valorSaldo, situacao=@situacao, nrDocOri=@nrDocOri,  cadastro=@cadastro, cadastradoPor=@cadastradoPor, edicao=@edicao, editadoPor=@editadoPor ");
			query.AppendLine("WHERE idTitRec=@idTitRec ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idTitRec", titrecVO.idTitRec));
			cmd.Parameters.Add(getParameter(cmd, "@idFilial", titrecVO.idFilial));
			if (titrecVO.idCtaCor == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", titrecVO.idCtaCor));
			if (titrecVO.idCaixa == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", titrecVO.idCaixa));
			if (titrecVO.idCliente == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCliente", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCliente", titrecVO.idCliente));
			if (titrecVO.idFormaPgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", titrecVO.idFormaPgto));
			if (titrecVO.idPlnCta == null)
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", titrecVO.idPlnCta));
			if (titrecVO.idCLucro == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCLucro", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCLucro", titrecVO.idCLucro));
			if (titrecVO.nrDoc == null)
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", titrecVO.nrDoc));
			if (titrecVO.obs == null)
				cmd.Parameters.Add(getParameter(cmd, "@obs", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@obs", titrecVO.obs));
			cmd.Parameters.Add(getParameter(cmd, "@vcto", titrecVO.vcto));
			if (titrecVO.pgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@pgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@pgto", titrecVO.pgto));
			cmd.Parameters.Add(getParameter(cmd, "@valor", titrecVO.valor));
			if (titrecVO.valorJuroC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", titrecVO.valorJuroC));
			if (titrecVO.valorDescC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", titrecVO.valorDescC));
			if (titrecVO.valorRec == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorRec", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorRec", titrecVO.valorRec));
			if (titrecVO.valorSaldo == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", titrecVO.valorSaldo));
			cmd.Parameters.Add(getParameter(cmd, "@situacao", titrecVO.situacao));
			if (titrecVO.nrDocOri == null)
				cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", titrecVO.nrDocOri));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", titrecVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", titrecVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", titrecVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", titrecVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}

		public long newTitrec(TitrecVO titrecVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO titrec ");
			query.AppendLine("  ( idFilial, idCtaCor, idCaixa, idCliente, idFormaPgto, idPlnCta, idCLucro, nrDoc, obs, vcto, pgto, valor, valorJuroC, valorDescC, valorRec, valorSaldo, situacao, nrDocOri, cadastro, cadastradoPor, edicao, editadoPor ) ");
			query.AppendLine("VALUES ( @idFilial, @idCtaCor, @idCaixa, @idCliente, @idFormaPgto, @idPlnCta, @idCLucro, @nrDoc, @obs, @vcto, @pgto, @valor, @valorJuroC, @valorDescC, @valorRec, @valorSaldo, @situacao, @nrDocOri, @cadastro, @cadastradoPor, @edicao, @editadoPor ) ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idFilial", titrecVO.idFilial));
			if (titrecVO.idCtaCor == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", titrecVO.idCtaCor));
			if (titrecVO.idCaixa == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", titrecVO.idCaixa));
			if (titrecVO.idCliente == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCliente", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCliente", titrecVO.idCliente));
			if (titrecVO.idFormaPgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", titrecVO.idFormaPgto));
			if (titrecVO.idPlnCta == null)
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", titrecVO.idPlnCta));
			if (titrecVO.idCLucro == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCLucro", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCLucro", titrecVO.idCLucro));
			if (titrecVO.nrDoc == null)
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", titrecVO.nrDoc));
			if (titrecVO.obs == null)
				cmd.Parameters.Add(getParameter(cmd, "@obs", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@obs", titrecVO.obs));
			cmd.Parameters.Add(getParameter(cmd, "@vcto", titrecVO.vcto));
			if (titrecVO.pgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@pgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@pgto", titrecVO.pgto));
			cmd.Parameters.Add(getParameter(cmd, "@valor", titrecVO.valor));
			if (titrecVO.valorJuroC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", titrecVO.valorJuroC));
			if (titrecVO.valorDescC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", titrecVO.valorDescC));
			if (titrecVO.valorRec == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorRec", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorRec", titrecVO.valorRec));
			if (titrecVO.valorSaldo == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", titrecVO.valorSaldo));
			cmd.Parameters.Add(getParameter(cmd, "@situacao", titrecVO.situacao));
			if (titrecVO.nrDocOri == null)
				cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", titrecVO.nrDocOri));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", titrecVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", titrecVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", titrecVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", titrecVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('titrec') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }

		}

		public void delTitrec(TitrecVO titrecVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM titrec ");
			query.AppendLine("WHERE idTitRec=@idTitRec ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idTitRec", titrecVO.idTitRec));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}
		public int getCountTitrecxIdFilial(int? idFilial)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFilial) as ocorrencias FROM titrec WHERE IdFilial=@IdFilial ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFilial", idFilial));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitrecxIdCtaCor(int? idCtaCor)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCtaCor) as ocorrencias FROM titrec WHERE IdCtaCor=@IdCtaCor ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCtaCor", idCtaCor));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitrecxIdCaixa(int? idCaixa)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCaixa) as ocorrencias FROM titrec WHERE IdCaixa=@IdCaixa ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCaixa", idCaixa));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitrecxIdCliente(int? idCliente)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCliente) as ocorrencias FROM titrec WHERE IdCliente=@IdCliente ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCliente", idCliente));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitrecxIdFormaPgto(int? idFormaPgto)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFormaPgto) as ocorrencias FROM titrec WHERE IdFormaPgto=@IdFormaPgto ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFormaPgto", idFormaPgto));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitrecxIdPlnCta(int? idPlnCta)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idPlnCta) as ocorrencias FROM titrec WHERE IdPlnCta=@IdPlnCta ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdPlnCta", idPlnCta));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitrecxIdCLucro(int? idCLucro)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCLucro) as ocorrencias FROM titrec WHERE IdCLucro=@IdCLucro ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCLucro", idCLucro));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
	}
}
